//
//  FlowControlVC.m
//  wanzhaoIM
//
//  Created by liuyuantao on 15/5/6.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "FlowControlVC.h"

#import "MicroDef.h"
#import "TextSwitchCell.h"
#import "CUserSettingMgr.h"
#import "CUserSettingModel.h"
//#import "CAccountMgr.h"
//#import "CAccountModel.h"
#import "CFolderMgr.h"


#define KSECTIONHEIGHT 20.0f



@interface FlowControlVC ()<UITableViewDelegate, UITableViewDataSource, TextSwitchCellDelegate>
{
    UITableView *_tableView;
}

@property (nonatomic, strong) CUserSettingModel *setting;
@property (nonatomic, strong) CAccountModel *acc;
@property (nonatomic, strong) CUserSettingMgr *setMgr;

@end

@implementation FlowControlVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationItem setHidesBackButton:YES];
    [self.navigationItem setNewTitleCenter:@"流量控制"];
    
    
    _tableView = [[UITableView alloc] init];
    _tableView.frame = CGRectMake(0, 0, HARDWARE_SCREEN_WIDTH, HARDWARE_SCREEN_HEIGHT - 64.0f);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = [UIColor clearColor];
    
    
    self.view.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
    self.setMgr = [[CUserSettingMgr alloc] init];
    self.setting = [self.setMgr getUserSetting];
    self.acc = [[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount];
    if (self.setting == nil) {
        self.setting = [[CUserSettingModel alloc] init];
        self.setting.userCode = self.acc.userName;
        [self.setMgr insetUserWithSetting:self.setting];
    }

    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/




#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    NSInteger rows = 0;
    
    switch (section) {
        case 0:
            rows = 1;
            break;
        default:
            rows = 0;
            break;
    }
    return rows;
}
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView              // Default is 1 if not implemented
//{
//    return 4;
//}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TextSwitchCell";
//    DLog(@"section:%zd, rows:%zd", indexPath.section, indexPath.row);
    UITableViewCell *cell = nil;
    if(indexPath.section == 0 && indexPath.row == 0)
    {
        TextSwitchCell  *textSwtichCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (textSwtichCell == nil) {
            textSwtichCell = [[[NSBundle mainBundle] loadNibNamed:@"TextSwitchCell" owner:self options:nil] objectAtIndex:0];
        }
        [textSwtichCell.titleLabel setText:[self titleName:indexPath]];
        textSwtichCell.switchController.on = self.setting.IsOnlyWifiRcv;
        [textSwtichCell.seperatorLine setHidden:YES];
        textSwtichCell.delegate = self;
        textSwtichCell.indexPath = indexPath;
        cell = textSwtichCell;
    }

    
    return cell;
}

-(NSString *)titleName:(NSIndexPath *)indexPath
{
    NSString *title = nil;
    
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0:
                    title = @"仅在连接wifi时自动接收图片";
                    break;
                default:
                    break;
            }
        }
            break;
        default:
            break;
    }
    return title;
}




#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 50.0f;
    
    if(indexPath.row == 0 && indexPath.section == 0)
    {
        height = 50.0f;
    }
    return height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.section) {
        case 1:
        {
            switch (indexPath.row) {
                case 0:
                    break;
                case 1:
                    break;
                default:
                    break;
            }
        }
            break;
        default:
            break;
    }
    
    
}

- (void)SwitchCellDidChangeSettingWithIndexPath:(NSIndexPath *)indexPath settingValue:(BOOL)settingValue
{
    self.setting.IsOnlyWifiRcv = settingValue;
    [self.setMgr updateUserSetting:self.setting];
    
    [_tableView reloadData];
}

@end
